package com.hckjsec.website.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteGuide;
import com.hckjsec.website.service.IWebSiteGuideService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 办证指南Controller
 * 
 * @author cyt
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/webguide")
public class WebSiteGuideController extends BaseController
{
    @Autowired
    private IWebSiteGuideService webSiteGuideService;

    /**
     * 查询办证指南列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteGuide webSiteGuide)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteGuide.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteGuide> list = webSiteGuideService.selectWebSiteGuideList(webSiteGuide);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出办证指南列表
     */
    @Log(title = "办证指南", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteGuide webSiteGuide)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteGuide.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteGuide> list = webSiteGuideService.selectWebSiteGuideList(webSiteGuide);
        ExcelUtil<WebSiteGuide> util = new ExcelUtil<WebSiteGuide>(WebSiteGuide.class);
        return util.exportExcel(list, "guide");
    }

    /**
     * 获取办证指南详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webSiteGuideService.selectWebSiteGuideById(id));
    }

    /**
     * 新增办证指南
     */
    @Log(title = "办证指南", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteGuide webSiteGuide)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteGuide.getContent(),""))){//解密
            webSiteGuide.setContent(Base64Converter.decode(webSiteGuide.getContent()));
        }else{
            webSiteGuide.setContent(webSiteGuide.getContent());
        }
        webSiteGuide.setDeptId(SecurityUtils.getDeptId());
        webSiteGuide.setOperator(SecurityUtils.getUserId());
        return toAjax(webSiteGuideService.insertWebSiteGuide(webSiteGuide));
    }

    /**
     * 修改办证指南
     */
    @Log(title = "办证指南", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteGuide webSiteGuide)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteGuide.getContent(),""))){//解密
            webSiteGuide.setContent(Base64Converter.decode(webSiteGuide.getContent()));
        }else{
            webSiteGuide.setContent(webSiteGuide.getContent());
        }
        return toAjax(webSiteGuideService.updateWebSiteGuide(webSiteGuide));
    }

    /**
     * 删除办证指南
     */
    @Log(title = "办证指南", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteGuideService.deleteWebSiteGuideByIds(ids));
    }
}
